package ltd.sshine.runtime.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import ltd.sshine.runtime.exception.ServiceException;
import ltd.sshine.runtime.exception.SystemRuntimeException;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

@Data
@Accessors(chain = true)
@Schema(description = "响应结果")
public class Result<T>{
    @Schema(description = "状态码")
    int code;
    @Schema(description = "消息")
    String msg;
    @Schema(description = "数据(如果存在)")
    @JsonInclude(NON_NULL)
    T data;
    public static <T> Result<T> success() {
        return success(null);
    }


    public static <T> Result<T> success(T data) {
        return message("ok",200,data);
    }

    public static<T> Result<T> error(String msg){
        return message(msg,500,null);
    }
    public static<T> Result<T> error(String msg,int code){
        return message(msg,code,null);
    }
    public static<T> Result<T> error(String msg,int code,T data){
        return message(msg, code, data);
    }
    public static<T> ServiceException except(String msg){
        return new ServiceException(msg,500);
    }
    public static<T> SystemRuntimeException runExcept(Throwable t, String msg){
        return new SystemRuntimeException(t,msg,500);
    }
    public static  <T> Result<T> message( String msg,int code,T data) {
        return new Result<T>().setMsg(msg).setCode(code).setData(data);
    }

}